﻿
using IRealize;
using Realize;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;

namespace WebApplication9.Controllers.ClientSet
{
    //职称设置
    public class ZCSZController : Controller
    {
        private readonly ILogger<ZCSZController> _logger;
        private readonly IConfiguration _configuration;

        public ZCSZController(ILogger<ZCSZController> logger, IConfiguration configuration)
        {
            _logger = logger;
            _configuration = configuration;
        }

        public IActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public async Task<IActionResult> GetTitles()//查询数据添加到前台
        {
            try
            {
                IConfig_Major_Zw config_Major = new Config_Major_Zw();
                List<Entity.Config_Major_Zw> zhi = await config_Major.select(_configuration["SqlServer"]);
                return Ok(new
                {
                    list = zhi
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"获取职称数据失败: {ex.Message}");
            }
        }

        [HttpGet]
        public async Task<IActionResult> GetTitleById(string id)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(id))
                {
                    return BadRequest("职称ID不能为空");
                }

                IConfig_Major_Zw config_Major = new Config_Major_Zw();
                var title = await config_Major.GetById(id, _configuration["SqlServer"]);

                if (title == null)
                {
                    return NotFound("未找到指定职称");
                }

                return Ok(title);
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"获取职称数据失败: {ex.Message}");
            }
        }


        [HttpDelete]
        public async Task<IActionResult> Delete(string id)//删除
        {
            try
            {
                IConfig_Major_Zw config_Major = new Config_Major_Zw();
                int result = await config_Major.Delete(id, _configuration["SqlServer"]);
                return Ok(new
                {
                    zhi = result
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"删除失败: {ex.Message}");
            }
        }

        [HttpGet]
        public IActionResult Add()
        {
            return View();
        }

        public IActionResult Fan()//返回
        {
            return RedirectToAction("Index");
        }

        [HttpPost]
        public async Task<IActionResult> AddTitle([FromBody] AddTitleRequest request)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(request.Name))
                {
                    return BadRequest(new { success = false, message = "职称名称不能为空" });
                }

                IConfig_Major_Zw config_Major = new Config_Major_Zw();
                int result = await config_Major.Add(request.Name, _configuration["SqlServer"]);

                if (result > 0)
                {
                    return Ok(new { success = true, message = "添加成功" });
                }
                else
                {
                    return Ok(new { success = false, message = "添加失败" });
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "添加职称失败");
                return StatusCode(500, new { success = false, message = "添加职称失败: " + ex.Message });
            }
        }

        [HttpPost]
        public async Task<IActionResult> Update(string major_zwid, string major_name)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(major_zwid) || string.IsNullOrWhiteSpace(major_name))
                {
                    TempData["ErrorMessage"] = "职称ID和名称不能为空";
                    return RedirectToAction("Upd", new { id = major_zwid });
                }

                IConfig_Major_Zw config_Major = new Config_Major_Zw();
                int result = await config_Major.Upd(major_zwid, major_name, _configuration["SqlServer"]);

                if (result > 0)
                {
                    TempData["SuccessMessage"] = "修改成功";
                }
                else
                {
                    TempData["ErrorMessage"] = "修改失败";
                }

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "修改职称失败");
                TempData["ErrorMessage"] = "修改职称失败: " + ex.Message;
                return RedirectToAction("Upd", new { id = major_zwid });
            }
        }

        // 添加请求模型
        public class AddTitleRequest
        {
            public string Name { get; set; }
        }

        [HttpGet]
        public IActionResult Upd(string id)
        {
            // 将ID传递到视图中
            ViewBag.TitleId = id;
            return View();
        }
    }
}